package com.scuec.tourismwebsite.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.scuec.tourismwebsite.entity.User;
import com.scuec.tourismwebsite.mapper.UserMapper;
import com.scuec.tourismwebsite.vo.UserPost;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper UserMapper;

    @Override
    public List<User> findUserAll() {
        return UserMapper.findUserAll();
    }

    @Override
    public int deleteUserById(int user_id) {
        return  UserMapper.deleteUserById(user_id);
    }

    @Override
    public int addUser(User user) {
        return UserMapper.addUser(user);
    }

    @Override
    public User getUserById(int user_id) {
        return UserMapper.getUserById(user_id);
    }

    @Override
    public User findUserByPhone(String phone_number) {
        return UserMapper.findUserByPhone(phone_number);
    }

    @Override
    public List<User> findUserByName(String nickname) {
        return UserMapper.findUserByName(nickname);
    }

    @Override
    public PageInfo<User> findUserPages(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<User> lists = UserMapper.findUserAll();
        PageInfo<User> pageInfo = new PageInfo<>(lists);
        return pageInfo;
    }

    @Override
    public PageInfo<User> findUserPage(String nickname, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<User> lists = UserMapper.findUserByName(nickname);
        PageInfo<User> pageInfo = new PageInfo<>(lists);
        return pageInfo;
    }

    @Override
    public PageInfo<User> bozhuPages(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<User> lists = UserMapper.findUserAll();
        PageInfo<User> pageInfo = new PageInfo<>(lists);
        return pageInfo;
    }

    @Override
    public PageInfo<User> bozhuPage(String nickname, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<User> lists = UserMapper.findUserByName(nickname);
        PageInfo<User> pageInfo = new PageInfo<>(lists);
        return pageInfo;
    }



    @Override
    public String getPhoneNumberByUserId(String phone_number) {
        User userObj = UserMapper.findUserByPhone(phone_number);
        return userObj != null ? String.valueOf(userObj.getUser_id()) : null;
    }
    @Override
    public boolean checkUserPassword(int user_id, String password) {
        User user = UserMapper.getUserById(user_id);
        return user != null && user.getPassword().equals(password);
    }

    @Override
    public boolean updatePassword(int user_id, String newPassword) {
        return UserMapper.updatePassword(user_id, newPassword) > 0;
    }

    @Override
    public int updateUser(User user) {
        return UserMapper.updateUser(user);
    }


    @Override
    public void updateUser(int user_id, String avatar) {
        User user = new User();
        user.setUser_id(user_id);
        user.setAvatar(avatar);
        UserMapper.updateUser(user);
    }
//
    @Override
    public List<UserPost> getUserPosts(int user_id) {
        return UserMapper.getUserPostsByUserId(user_id);
    }

    @Override
    public User loginByPhone(String phone, String password) {
        // 根据手机号查找用户并验证密码
        User user = UserMapper.findByPhoneNumber(phone);
        if (user != null && user.getPassword().equals(password)) {
            return user; // 返回用户对象
        }
        return null; // 用户不存在或密码错误
    }
    @Override
    public int getTotalUsers() {
        return UserMapper.countUsers();
    }

    @Override
    public int updateFollowersCount(@Param("followers_count")int followers_count, @Param("user_id")int user_id){
        return UserMapper.updateFollowersCount(followers_count, user_id);
    }

    @Override
    public int updateFollowingCount(@Param("following_count")int following_count,@Param("user_id") int user_id) {
        return UserMapper.updateFollowingCount(following_count, user_id);
    }
}
